package modelcontroller;

import gui.ChatGUI;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import network.ChatNI;

/**
 *
 * @author Mariam
 */
public class ChatController {

    private ChatModel model;
    private ChatGUI gui;
    private ChatNI ni;
    private String filepath;

    public ChatController(ChatModel model, ChatGUI gui, ChatNI ni) {
        this.model = model;
        this.gui = gui;
        this.ni = ni;
    }

    public void performConnect(String username) {
        try {
            User localUser = new User(username, InetAddress.getLocalHost().getHostName());
            model.setLocalUser(localUser);
            model.setLocalUserIsConnected(true);
        } catch (UnknownHostException ex) {
            Logger.getLogger(ChatController.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void performDisconnect() {
        model.setLocalUserIsConnected(false);
    }

    public void performUserSelected(User remoteUser) {
        Conversation c = new Conversation(remoteUser);
        model.addConversations(c);
        gui.addTab(c);
    }

    public void performHelloRcvd(String username, String hostname) {
        User newUser = new User(username, hostname);
        model.addUserInList(newUser);
    }

    public void performMsgRcvd(User remoteUser, String msg) {
        Conversation c = model.getHashmapConv().get(remoteUser);//new Conversation(new User(username,hostname));
        String username = remoteUser.getUsername();
        if (c != null) //cs.getModel().getConversations().addElement(c);
        {
            c.addMessage(username + " :  " + msg);
            model.addConversations(c);
        } else {
            c = model.createConversation(remoteUser);
            gui.addTab(model.getHashmapConv().get(remoteUser));
            c.addMessage(username + " :  " + msg);
            model.addConversations(c);
        }


    }

    public void performByeRcvd(User remoteUser) {
        model.removeUserInList(remoteUser);
        String username = remoteUser.getUsername();
        Conversation c = model.getHashmapConv().get(remoteUser);
        if (c != null) {
            c.addMessage(username + " : ---- DISCONNECTED ----");
            model.addConversations(c);
        }
    }

    public void performByeSent() {
        gui.close();
    }

    public void performFileRequestRcvd(String username, String filename, long size) {
        boolean accepted = gui.notifyFileRequest(username, filename);
        performSendFileAnswer(username, filename, accepted, size);
    }

    public void performFileAnswerRcvd(String username, String filename, boolean accepted) {
        String message;
        Conversation c = model.getHashmapConv().get(model.getHashmapUsers().get(username));
        if (!accepted) {
            message = ": I don't want to download the file '";
        } else {
            message = ": I want to download the file '";
        }
        c.addMessage(username + message + filename + "'");
        model.addConversations(c);
        if (accepted)
            performSendFile(username);
    }

    public void performSendMessage(String username, String message) {
        ni.sendMsg(username, message);
        Conversation c = model.getHashmapConv().get(model.getHashmapUsers().get(username));
        c.addMessage(model.getLocalUser().getUsername() + " :  " + message);
        model.addConversations(c);
    }

    public void performSendFileRequest(String username, String filename, String filepath, long size) {
        ni.sendFileRequest(username, filename, size);
        Conversation c = model.getHashmapConv().get(model.getHashmapUsers().get(username));
        c.addMessage(model.getLocalUser().getUsername() + " :  I want to send you this file '" + filename + "'");
        model.addConversations(c);
        this.filepath = filepath;
    }

    public void performSendFileAnswer(String username, String filename, boolean accepted, long size) {
        String message;
        
        Conversation c = model.getHashmapConv().get(model.getHashmapUsers().get(username));
        if (!accepted) {
            message = ": I don't want to download the file '";
        } else {
            message = ": I want to download the file '";
            filepath = performAskFilepath();
        }
        c.addMessage(model.getLocalUser().getUsername() + message + filename + "'");
        model.addConversations(c);
        if (accepted) {
            ni.receiveFile(filename, filepath);
            c.addMessage(model.getLocalUser().getUsername() + " :  FILE RECEIVED ");
            model.addConversations(c);
        }
        ni.sendFileAnswer(username, filename, accepted, size);
    }

    public void performSendFile(String username) {
        ni.sendFile(username, this.filepath);
        Conversation c = model.getHashmapConv().get(model.getHashmapUsers().get(username));
        c.addMessage(model.getLocalUser().getUsername() + " :  FILE SENT");
        model.addConversations(c);
    }

    public String performAskFilepath() {
        return gui.askFilepath();
    }
}
